<?php
include_once("../Entities/QuestionResult.php");
include_once("WebSession.php");

class FunctionUtilities
{
	public static function CheckAnswers($questionId, $answerList)
	{
		$questionResult = WebSession::GetSession("QuestionResult");
		if($questionResult != null)
		{
			for($i = 0; $i < count($questionResult); $i++)
			{
				if($questionResult[$i]->QuestionId == $questionId)
				{
					$answerIds = split("_", $questionResult[$i]->AnswersList);
					for($j = 0; $j < count($answerIds); $j++)
					{
						for($as = 0; $as < count($answerList); $as++)
						{
							if($answerList[$as]->Id == $answerIds[$j])
							{
								$answerList[$as]->IsChecked = true;
								break;
							}
						}
					}
				}
			}
		}
		return $answerList;
	}
	
	public static function DeleteFile($filePath)
	{
		if(file_exists($filePath))
			return unlink($filePath);
	}
	
	public static function GetFileExtension($filename)
	{
		$filename = strtolower($filename) ;
		$exts = split("[/\\.]", $filename) ;
		$n = count($exts)-1;
		$exts = $exts[$n];
		return $exts; 
	}
	
	public static function SaveUploadSound($file, $isQuestion) 
	{
		if($file != null)
		{
			$extension = FunctionUtilities::GetFileExtension($file['name']);
			if($extension == 'mp3' || $extension == 'wma')
			{
				$newName = md5(date('dmyGisu')) . "." . $extension;
				$desPath = "";
				if($isQuestion)
				{
					$desPath = "../sound/questions/" . $newName;
				}
				else
				{
					$desPath = "../sound/paragraphs/" . $newName;
				}
				move_uploaded_file($file['tmp_name'], $desPath);
				return $desPath;
			}	
		}
		return "";
	} 
}

?>